﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace KaiXin.BLL
{
    /// <summary>
    /// 代理机器人
    /// </summary>
    public class Robot
    {
        #region Field
        private WebHelper _wh;
        private bool _isPublic = false;
        #endregion

        #region Property
        public string Email { get; private set; }
        public string Password { get; private set; }
        #endregion

        public Robot(string email, string pwd)
        {
            Email = email;
            Password = pwd;
            _wh = new WebHelper(Email);
        }
        /// <summary>
        /// 默认构造，公共数据账号登陆
        /// </summary>
        public Robot()
        {
            _isPublic = true;
        }

        /// <summary>
        /// 模拟访问页面
        /// </summary>
        /// <param name="url"></param>
        /// <returns>html的包装类</returns>
        protected HtmlDocument AccessPage(string url)
        {
            if (_isPublic)
            {
                return PublicRobotFactory.GetPublicRobot().AccessPage(url);
            }
            else
            {
                HtmlDocument doc = _wh.GetHtmlWeb().Load(url);
                if (Common.IsLogin(doc))
                {
                    return doc;
                }
                else
                {
                    if (Common.Login(Email, Password))
                    {
                        return _wh.GetHtmlWeb().Load(url);
                    }
                    else
                    {
                        throw new Exception("账号密码错误");
                    }
                }
            }
            
        }


        #region private Factory class
        /// <summary>
        /// 采用私有工厂类随机返回多个公用账号
        /// </summary>
        private class PublicRobotFactory
        {
            #region private
            private static List<Robot> _listRobot = null;
            //公共数据获取账号
            private static readonly string[] _emails = { 
                                                "zachary01@yahoo.cn", 
                                                "zachary02@yahoo.cn", 
                                                "zachary03@yahoo.cn", 
                                                "zachary04@yahoo.cn", 
                                                "zachary05@yahoo.cn"
                                            };
            private static readonly Random _random = new Random();
            #endregion
            //静态构造类
            static PublicRobotFactory()
            {
                _listRobot = new List<Robot>();
                foreach (var email in _emails)
                {
                    _listRobot.Add(new Robot(email, "123456"));//账号统一密码
                }
            }

            public static Robot GetPublicRobot()
            {
                int index = _random.Next(_emails.Length);
                return _listRobot[index];
            }
        }
        #endregion

        
    }
}
